// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "SETSParser.h"

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <map>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DataStructure/Input/Parameter/SETSData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Parameter/ISETSService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (SET);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::Import::BDF;
using namespace SG::DataStructure;

SG::DataStructure::Common::Status BDF::BULKDATA::ParseSET (const std::shared_ptr<DBServiceFactory>& dbServiceFactory,
                                                           std::vector<std::string>&                dividedEntryInfo)
{
    //

    FEM::SETSData tempData;

    tempData.m_domainId = 1;
    tempData.m_sId      = -1;  // 默认的SId为-1，表示全局Set 后面复制到每个工况去
    tempData.m_setId    = convertToI64 (dividedEntryInfo[0]);

    bool lastTHRU = false;

    for (size_t i = 1; i < dividedEntryInfo.size (); i++)
    {
        if (boost::icontains (dividedEntryInfo[i], "THRU"))
        {
            lastTHRU = true;
            continue;
        }

        if (!dividedEntryInfo[i].empty ())
        {
            if (lastTHRU)
            {
                lastTHRU = false;
                tempData.m_memeIds.push_back (-convertToI64 (dividedEntryInfo[i]));
            }
            else
            {
                tempData.m_memeIds.push_back (convertToI64 (dividedEntryInfo[i]));
            }
        }
    }

    // 将解析完成的节点数据添加到数据库服务中
    return dbServiceFactory->get<ISETSService> ()->Add (tempData);
}